package org.jeecg.modules.tls.csmg.cscourse.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.tls.blog.common.ResponseResult;
import org.jeecg.modules.tls.csmg.cscourse.entity.CsCourse;
import org.jeecg.modules.tls.csmg.cscourse.service.ICsCourseService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.tls.csmg.viewmodel.admin.CourseItemsVM;
import org.jeecg.modules.tls.csmg.viewmodel.admin.CourseRequestVM;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.tls.csmg.viewmodel.user.CourseUserVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: cs_course
 * @Author: jeecg-boot
 * @Date:   2022-08-05
 * @Version: V1.0
 */
@Api(tags="cs_course")
@RestController
@RequestMapping("/cscourse/csCourse")
@Slf4j
public class CsCourseController extends JeecgController<CsCourse, ICsCourseService> {
	@Autowired
	private ICsCourseService csCourseService;
	
	/**
	 * 分页列表查询
	 *
	 * @param csCourse
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "cs_course-分页列表查询")
	@ApiOperation(value="cs_course-分页列表查询", notes="cs_course-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CourseItemsVM>> queryPageList(CsCourse csCourse,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="16") Integer pageSize,
													  HttpServletRequest req) {
		QueryWrapper<CsCourse> queryWrapper = QueryGenerator.initQueryWrapper(csCourse, req.getParameterMap());
		Page<CsCourse> page = new Page<CsCourse>(pageNo, pageSize);
		return Result.OK(csCourseService.getCourseList(page,queryWrapper));
	}
	
	/**
	 *   添加
	 *
	 * @param courseRequestVM
	 * @return
	 */
	@AutoLog(value = "cs_course-添加")
	@ApiOperation(value="cs_course-添加", notes="cs_course-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CourseRequestVM courseRequestVM) {
		if(csCourseService.addCourse(courseRequestVM)){
			return Result.OK();
		}else {
			return Result.error("添加失败");
		}

	}
	
	/**
	 *  编辑
	 *
	 * @param csCourse
	 * @return
	 */
	@AutoLog(value = "cs_course-编辑")
	@ApiOperation(value="cs_course-编辑", notes="cs_course-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CsCourse csCourse) {
		csCourseService.updateById(csCourse);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "cs_course-通过id删除")
	@ApiOperation(value="cs_course-通过id删除", notes="cs_course-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		csCourseService.deleteCourse(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "cs_course-批量删除")
	@ApiOperation(value="cs_course-批量删除", notes="cs_course-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		List<String> idList = Arrays.asList(ids.split(","));
		idList.stream().forEach(id->csCourseService.deleteCourse(id));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "cs_course-通过id查询")
	@ApiOperation(value="cs_course-通过id查询", notes="cs_course-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CourseRequestVM> queryById(@RequestParam(name="id",required=true) String id) {
		CsCourse csCourse = csCourseService.getById(id);
		if(csCourse==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(csCourseService.getCourse(id));
	}

    /**
    * 导出excel
    *
    * @param request
    * @param csCourse
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CsCourse csCourse) {
        return super.exportXls(request, csCourse, CsCourse.class, "cs_course");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CsCourse.class);
    }

	@GetMapping(value = "/getCourseUser")
	public Result<CourseUserVM> getCourseUserInfo(@RequestParam(name="id",required=true) String id){
		return Result.ok(csCourseService.getCourseUserVMBYId(id));
	}


	@GetMapping(value = "/courseList")
	public ResponseResult courseList(){
    	return csCourseService.getCourseList();
	}
}
